<script setup lang="ts">
import { useUserStore } from "@/stores/user";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

// 获取存储库
const store = useUserStore();
// 获取路由器
const router = useRouter();
// 点击退出登录
const onLogout = () => {
  // 确认弹窗
  ElMessageBox.confirm(
    // 内容
    "确定退出登录吗？",
    // 标题
    "提示",
    {
      confirmButtonText: "确定", // 确定按钮文案
      cancelButtonText: "取消", // 取消按钮文案
      type: "warning", // 图标类型
    }
  )
    // 点击确定
    .then(() => {
      // 删除 token（内存+本地）
      store.removeToken();
      // 跳转登录页
      router.replace("/login");
      // 用户提示
      ElMessage({ message: "退出成功", type: "success" });
    })
    // 点击取消
    .catch(() => {});
};
</script>

<template>
  <div class="nav-bar">
    <!-- breadcrumb -->
    <div></div>
    <!-- dropdown -->
    <el-dropdown>
      <div class="user-wrapper el-dropdown-link">
        黑马管理员
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item>首页</el-dropdown-item>
          <el-dropdown-item>项目地址</el-dropdown-item>
          <el-dropdown-item @click="onLogout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<style lang="scss" scoped>
.nav-bar {
  height: 60px;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;

  .user-wrapper {
    outline: none;
    cursor: default;
    color: var(--el-color-black);
  }
}
</style>
